*==============================================================================*
* Regression Analysis
*==============================================================================*

ssc install coefplot, replace
ssc install scheme-burd, replace
ssc install winsor2, replace
set scheme burd

/* if we want to change the mean value on the plot, the code should be run in sections */
*==============================================================================*
* Weighted interest rates
cd "P:\ECB business areas\DGR\Staff\papouts\Research\Active\Firm Heterogeneities"
global project "P:\ECB business areas\DGR\Staff\papouts\Research\Active\Firm Heterogeneities"
global data "${project}\Final datasets\"
global output "${project}\Final charts\Revision"

use "${data}\instr_lvl_dt_f_newloans.dta", clear

* drop missing wir
drop if wir_byloantype == .

sum wir_byloantype, d

*Drop missing SIZE classification
tab sz_f
drop if sz_f == . 

*Drop missing NACE code
drop if nace_code == ""
tab nace_code
egen sctr_f = group(nace_code)

*Drop missing CNTRY code
drop if cntry_dbtr == ""
tab cntry_dbtr
drop if inlist(cntry_dbtr, "CY", "EE", "LT", "LU", "LV", "MT", "SI", "SK")
encode cntry_dbtr, gen(cntry_f)

* drop instruments different from loans and credit lines
drop if inlist(typ_instr_corr, "Revolving credit", "Finance leases", "Trade receivables")
tab typ_instr_corr

*Loan type
encode typ_instr_corr, gen(instr_f)

* calculate loan type share within debtor
egen dbtr_ona = total(ona_intrate_c_ttl), by(dbtr_id)
gen ONA_DBTR_LOAN_SHR = ona_intrate_c_ttl/dbtr_ona

* drop if missing loan share
sum ONA_DBTR_LOAN_SHR
drop if ONA_DBTR_LOAN_SHR == .

* mean value
sum wir_byloantype

/*
tab CNTRY

preserve 
collapse W_RT_BYTYPE [aweight=ONA_DBTR_LOAN_SHR],by(CNTRY)
list 
restore
*/


*==============================================================================*
* interest rates
*==============================================================================*

* unconditional regression
reg wir_byloantype  ibn.cntry_f [aweight=ONA_DBTR_LOAN_SHR], noconstant baselevels
estimates store r_uncond

* testing the equality of multiple coefficients
testparm i(1/11).cntry_f, equal

matrix coeff3 = e(b)'
matrix list coeff3
svmat coeff3
gen n = _n
preserve
	keep n coeff31
	keep if n >= 1 & n <12
	save "${data}\uncond_coeff_newlterm.dta", replace
	egen mu_uncond = mean(coeff31)
	drop n coeff31
	duplicates drop *, force
	gen n = _n
	save "${data}\uncond_newlterm.dta", replace
restore

	
translate @Results "${output}\6_reg_intrate_unw_newlterm_logfile.txt", replace



